package com.github.postsanf.yinian.activity;

import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.bean.YNAlbum;
import com.github.postsanf.yinian.largeImageView.InputStreamBitmapDecoderFactory;
import com.github.postsanf.yinian.largeImageView.LargeImageView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class TimeLineStatusFristHuoDongActivity extends BaseActivity implements View.OnClickListener {
    private YNAlbum curAlbum;
    private LargeImageView iv_pc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_timeline_topstatushuodong);
        curAlbum = (YNAlbum) getIntent().getSerializableExtra("StatusFrist");
        url = curAlbum.getIntroducePic();
        initView();
    }

    String url="";
    File f;
    Handler downloadHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 2) {
                try {
                    if (f != null) {
                        iv_pc.setImage(new InputStreamBitmapDecoderFactory(new FileInputStream(f)));
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (f != null && f.exists())
            f.delete();
    }

    private void initView() {
        findViewById(R.id.top_back).setOnClickListener(this);
        findViewById(R.id.top_back_time).setOnClickListener(this);
        iv_pc = (LargeImageView) findViewById(R.id.iv_pc);
        if (!url.contains("http")) {
            url = "http://" + url;
        }
        File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), "yinian/YiNian");
        if (!(file.mkdir() || file.isDirectory())) {
            file.mkdirs();
        }
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    URL url1 = new URL(url);
                    HttpURLConnection conn = (HttpURLConnection) url1.openConnection();
                    conn.setConnectTimeout(5000);
                    conn.setRequestMethod("GET");
                    int code = conn.getResponseCode();
                    if (code == 200) {
                        InputStream is = conn.getInputStream();
                        Long tsLong = System.currentTimeMillis() / 1000;
                        String ts = "yn_" + tsLong.toString() + ".png";
                        String localPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/yinian/YiNian/" + ts;
                        f = new File(localPath);
                        if (f.exists()) {
                            f.delete();
                        }
                        FileOutputStream fos = new FileOutputStream(f);
                        int len = 0;
                        byte[] buff = new byte[1024];
                        while ((len = is.read(buff)) != -1) {
                            fos.write(buff, 0, len);
                        }
                        is.close();
                        fos.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                downloadHandler.sendEmptyMessage(2);
            }
        }.start();
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.top_back:
            case R.id.top_back_time:
                finish();
                break;
        }
    }
}
